package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.web.entity.TellHistoryVideo;
import com.dingreading.cloud.web.entity.table.TellHistoryVideoTableDef;
import com.dingreading.cloud.web.mapper.TellHistoryVideoMapper;
import com.dingreading.cloud.web.service.TellHistoryVideoService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-04-07
 */
@Service
public class TellHistoryVideoServiceImpl extends BaseServiceImpl<TellHistoryVideoMapper, TellHistoryVideo, TellHistoryVideoTableDef> implements TellHistoryVideoService {

    @Override
    protected TellHistoryVideoTableDef getTable() {
        return TellHistoryVideoTableDef.tellHistoryVideo;
    }

    @Override
    public Page<TellHistoryVideo> pageList(PageUtil pageUtil, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.name.like(keyword)
            );
        }
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public int getMaxOrderNum() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table);
        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public TellHistoryVideo getLtShowOrder(Long id, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id))
                .and(table.orderNum.lt(orderNum));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public TellHistoryVideo getGtShowOrder(Long id, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id))
                .and(table.orderNum.gt(orderNum));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public void updateShowOrder(Long id, Integer orderNum) {
        UpdateChain.of(TellHistoryVideo.class)
                .set(table.orderNum, orderNum)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<TellHistoryVideo> getList(Integer enabled) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.enabled.eq(enabled))
                .orderBy(table.orderNum.asc());

        return getMapper().selectListByQuery(wrapper);
    }
}
